﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Weber.CS3280.Inventory.Logic
{
	public static class EnumerableExtensions
	{
		/// <summary>
		/// Subscribes an event handler to each item in the passed in list
		/// </summary>
		/// <param name="list">List that will be subscribed</param>
		/// <param name="handlerDelegate">Reference to the delegate 
		/// that handles the event</param>
		public static void SubscribeNotifyChanged(
			this IEnumerable<INotifyPropertyChanged> list, 
			PropertyChangedEventHandler handlerDelegate)
		{
			foreach (INotifyPropertyChanged item in list)
			{
				item.PropertyChanged += handlerDelegate;
			}
		}

		/// <summary>
		/// Unsubscribes an event handler to each item in the passed in list
		/// </summary>
		/// <param name="list">List that will be unsubscribed</param>
		/// <param name="handlerDelegate">Reference to the existing delegate 
		/// that handles the event</param>
		public static void UnsubscribeNotifyChanged(
			this IEnumerable<INotifyPropertyChanged> list,
			PropertyChangedEventHandler handlerDelegate)
		{
			// Iterate through each item in the list and unsubscribe it
			foreach (INotifyPropertyChanged item in list)
			{
				item.PropertyChanged -= handlerDelegate;
			}
		}
	}
}
